![]() |
PATH![]() |
![]() ![]() |
Normally, if a child script object and its parent both have handlers for the same command, the child uses its own handler. However, the handler in a child script object can handle a command first, and then use a Continue statement to call the handler for the same command in the parent.
The use of a Continue statement to call a handler in a parent script object is called delegation. By delegating commands to a parent script object, a child can extend the behavior of a handler contained in the parent without having to repeat the entire handler definition. After the parent handles the command, AppleScript continues at the place in the child where the Continue statement was called. Handlers in child script objects that contain Continue statements are similar to wrapper methods in object-oriented programming.
The syntax of a Continue statement is
continue commandName parameterList
commandName is the name of the current command.
parameterList is the list of parameters to be passed with the command. The list must follow the same format as the parameter definitions in the handler definition for the command. For handlers with labeled parameters, this means that the parameter labels must match those in the handler definition. For handlers with positional parameters, the parameters must appear in the correct order. You can list actual values or parameter variables. If you list actual values, those values replace the parameter values that were specified in the original command. If you list parameter variables, the Continue statement passes the parameter values that were specified in the original command.
The following script includes two script object definitions similar to those shown in Figure 9-1. The first, Elizabeth , works just like the script John in the figure. The second, ChildOfElizabeth , includes a handler with a Continue statement that is not included in the child script object ( Simple ) shown in the figure.
script Elizabeth
property HowManyTimes : 0
to sayHello to someone
set HowManyTimes to HowManyTimes + 1
return "Hello " & someone
end sayHello
end script
script ChildOfElizabeth
property parent : Elizabeth
on sayHello to someone
if my HowManyTimes > 3 then
return "No, I'm tired of saying hello."
else
continue sayHello to someone
end if
end sayHello
end script
tell Elizabeth to sayHello to "Matt"
--result: "Hello Matt", no matter how often the tell is executed
tell ChildOfElizabeth to sayHello to "Bob"
--result: "Hello Bob", the first four times the tell is executed;
-- after the fourth time: "No, I'm tired of saying hello."
In the preceding example, the handler defined by ChildOfElizabeth for the sayHello command checks the value of the HowManyTimes property each time the handler is run. If the value is greater than 3, ChildOfElizabeth returns a message refusing to say hello. Otherwise, ChildOfElizabeth calls the sayHello handler in the parent script object ( Elizabeth ), which returns the standard hello message. The word someone in the Continue statement is a parameter variable. It indicates that the parameter received with the original sayHello command will be passed to the handler in the parent script.
Note
The reserved word my in the statement if my HowManyTimes > 10 in the previous example is required to indicate that HowManyTimes is a property of the script object. Without the word my , AppleScript assumes that HowManyTimes is an undefined local variable.
A Continue statement can change the parameters of a command before delegating it. For example, suppose the following script object is defined in the same script as the preceding example. The first Continue statement changes the direct parameter of the sayHello command from "Bill" to "William" . It does this by specifying the value "William" instead of the parameter variable someone .
script AnotherChildOfElizabeth
property parent : Elizabeth
on sayHello to someone
if someone = "Bill" then
continue sayHello to "William"
else
continue sayHello to someone
end if
end sayHello
end script
tell AnotherChildOfElizabeth to sayHello to "Matt"
--result: "Hello Matt"
tell AnotherChildOfElizabeth to sayHello to "Bill"
--result: "Hello William"
If you override a parent's handler in this manner, the reserved words me and my in the parent's handler no longer refer to the parent, as demonstrated in the example that follows.
script Hugh
on identify()
me
end identify
end script
script Andrea
property parent : Hugh
on identify()
continue identify()
end identify
end script
tell Hugh to identify()
--result: «script Hugh»
tell Andrea to identify()
--result: «script Andrea»